package service

import (
	"context"
	"fmt"
	"video-srv/basic/config"
	__ "video-srv/basic/proto/rpc"
	"video-srv/handler/model"
)

func (s *VideoServer) Register(_ context.Context, in *__.RegisterReq) (*__.RegisterResp, error) {
	smsCode, _ := config.Rdb.Get(config.Ctx, "smsCode_"+in.Phone).Result()
	if in.SmsCode != smsCode {
		return nil, fmt.Errorf("验证码错误")
	}
	user := model.User{
		Phone:    in.Phone,
		Username: in.Username,
		Password: in.Password,
	}
	err := user.Register(config.DB)
	if err != nil {
		return nil, err
	}
	return &__.RegisterResp{
		UserId: int64(user.ID),
	}, nil
}
func (s *VideoServer) Login(_ context.Context, in *__.LoginReq) (*__.LoginResp, error) {
	var user model.User
	err := user.IsPhone(config.DB, in.Phone)
	if err != nil {
		return nil, err
	}
	smsCode, _ := config.Rdb.Get(config.Ctx, "smsCode_"+in.Phone).Result()
	if in.SmsCode != smsCode {
		return nil, fmt.Errorf("验证码错误")
	}
	return &__.LoginResp{
		UserId:    int64(user.ID),
		Username:  user.Username,
		AvatarUrl: user.AvatarUrl,
	}, nil
}
